import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_lqy_demo/page/dynamic/dynamic_page.dart';
import 'package:flutter_lqy_demo/page/home/home_main_page.dart';
import 'package:flutter_lqy_demo/page/me/my_page.dart';
import 'package:flutter_lqy_demo/page/trend/my_page.dart';
import 'package:flutter_lqy_demo/widget/gsy_tabbar_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 主页

class HomePage extends StatefulWidget {
  static const String sName = "home";

  const HomePage({super.key});

  @override
  HomePageState createState() => HomePageState();
}

class HomePageState extends State<HomePage> {
  final GlobalKey<DynamicPageState> dynamicKey = GlobalKey();
  final GlobalKey<TrendPageState> trendKey = GlobalKey();
  final GlobalKey<MePageState> myKey = GlobalKey();

  /// 不退出
  _dialogExitApp(BuildContext context) async {
    ///如果是 android 回到桌面
    if (Platform.isAndroid) {
      // AndroidIntent intent = const AndroidIntent(
      //   action: 'android.intent.action.MAIN',
      //   category: "android.intent.category.HOME",
      // );
      // await intent.launch();
    }
  }

  _renderTab(String iconPath, text) {
    return Tab(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image(
            image: AssetImage(
              iconPath,
            ),
            width: 16.w,
            height: 16.w,
          ),
          Text(
            text,
            style: TextStyle(
              fontSize: 10.sp,
            ),
          )
        ],
      ),
    );
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    List<Widget> tabs = [
      _renderTab("assets/images/icon_home.png", "Home"),
      _renderTab("assets/images/icon_dynamic.png", "Dynamic"),
      _renderTab("assets/images/icon_me.png", "My"),
    ];

    ///增加返回按键监听
    return PopScope(
      canPop: false,
      onPopInvokedWithResult: (didPop, _) {
        _dialogExitApp(context);
      },
      //包含主页和底部样式
      child: GSYTabBarWidget(
        // drawer: const HomeDrawer(),

        type: TabType.bottom,
        tabItems: tabs,
        tabViews: [
          const HomeMainPage(),
          DynamicPage(key: dynamicKey),
          MePage(key: myKey),
        ],
        // onDoublePress: (index) {
        //   switch (index) {
        //     case 0:
        //       dynamicKey.currentState?.scrollToTop();
        //       break;
        //     case 1:
        //       // trendKey.currentState?.scrollToTop();
        //       break;
        //     case 2:
        //       // myKey.currentState?.scrollToTop();
        //       break;
        //   }
        // },

        indicatorColor: Colors.transparent,
        // title: GSYTitleBar(
        //   "Page title",
        //   iconData: GSYICons.MAIN_SEARCH,
        //   needRightLocalIcon: true,
        //   onRightIconPressed: (centerPosition) {
        //     // NavigatorUtils.goSearchPage(context, centerPosition);
        //   },
        // ),
      ),
    );
  }
}
